import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

//存放全局变量
const state = {
	catchPages: ["index", "home", "find", "about"],
	marketApi: "http://api.yuzhou14.com/",
	musicList: "",
	music: false
};

//同computed一样，每次都会执行，异步处理数据
const getters = {
	catchPages(context) {
		return context.catchPages;
	}
};

//存放全局方法,同步
const mutations = {
	add(state, item) {
		if(state.catchPages.indexOf(item) === -1) {
			state.catchPages.push(item);
		}
	},
	remove(state, item) {
		let index = state.catchPages.indexOf(item);
		if(index > 0) {
			state.catchPages.splice(index, 1)
		}
	},
	Listmusic(state, obj) {
		state.musicList = obj
	}
};

//异步处理
const actions = {
	add(state, item) {
		state.commit('add', item)
	},
	remove(state, item) {
		state.commit('remove', item)
	},
	Listmusic(state, obj) {
		state.commit('Listmusic', obj)
	}
};

export default new Vuex.Store({
	state,
	getters,
	mutations,
	actions
});